﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Security.Cryptography;
using System.Configuration;

namespace ZG_member
{
    public partial class UserAdd : Form
    {
        OperatorInfo operat;
        public UserAdd(OperatorInfo opera)
        {
            InitializeComponent();
            operat = opera;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];

            this.Opacity = 0;
        }

       
        private void UserAdd_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            if (operat.ID != 0)
            {
                this.Text = "修改操作员信息";
            }
            cbxsex.Items.Add("男");
            cbxsex.Items.Add("女");
            if (this.Text != "修改操作员信息")
            {
                label8.Visible = false;
                radioButton1.Visible = false;
                radioButton2.Visible = false;
                label2.Visible = true;
                label3.Visible = true;
                txtpassword.Visible = true;
                txtpassword1.Visible = true;
            }
            rollInfo();
        }
        /// <summary>
        /// 得到所有的角色名字
        /// </summary>
        private void rollInfo()
        {
            roleInfo info = new roleInfo();
            DataTable dt = info.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!cbxroleId.Items.Contains(dt.Rows[i]["roleName"].ToString()))
                {
                    cbxroleId.Items.Add(dt.Rows[i]["roleName"].ToString());
                }
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
           if (txtusername.Text == string.Empty)
           { MessageBox.Show("用户名不能为空"); return; }
           if(txtpassword.Text!=txtpassword1.Text)
           { MessageBox.Show("您输入的两次密码不一致"); return; }
           if(txtname.Text==string.Empty)
           { MessageBox.Show("姓名不能为空"); return; }
           if (cbxsex.Text == string.Empty)
           { MessageBox.Show("姓别不能为空"); return; }
           if ( txtphone.Text== string.Empty)
           { MessageBox.Show("电话不能为空"); return; }
           if (cbxroleId.Text == string.Empty)
           { MessageBox.Show("用户角色不能为空"); return; }

            operatorInfo info = new operatorInfo();
            DataTable dt = info.GetList("").Tables[0];
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    if (dt.Rows[i]["username"].ToString() == txtusername.Text)
            //    {
            //        MessageBox.Show("该用户名已存在，请重新填写用户名!"); return;
            //    }
            //}
            Model.operatorInfo infoModel = new Model.operatorInfo();
            if (this.Text == "修改操作员信息")
            {
                infoModel = info.GetModelList("id='" + operat.ID + "'")[0];
            }
            infoModel.username = txtusername.Text.Trim();
            if (txtpassword.Visible == true)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bt = Encoding.Default.GetBytes(txtpassword.Text);
                bt = md5.ComputeHash(bt);
                string pwd = BitConverter.ToString(bt).Replace("-", "");
                infoModel.password = pwd;
            }
            infoModel.name = txtname.Text.Trim();
            infoModel.phone = txtphone.Text.Trim();
            if (cbxsex.Text.Trim() == "男")
            {
                infoModel.sex = true;
            }
            else
            {
                infoModel.sex = false;
            }
            roleInfo  role = new roleInfo ();
            Model.roleInfo roleModel = new Model.roleInfo();
            roleModel = role.GetModelList("roleName='" + cbxroleId.Text.Trim() + "'")[0];
            infoModel.roleId = roleModel.id;
            if (this.Text == "修改操作员信息")
            {
                if (info.Update(infoModel))
                {
                    MessageBox.Show("修改成功!");
                    operat.dgvoperatorInfoBind();
                    this.Close();
                }
            }
            else
            {
                int row = info.Add(infoModel);
                if (row > 0)
                {
                    MessageBox.Show("添加成功!");
                    operat.dgvoperatorInfoBind();
                    this.Close();
                }
            }
        }

        

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void UserAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

                this.label2.Visible = true;
                this.txtpassword.Visible = true;
                this.label3.Visible = true;
                this.txtpassword1.Visible = true;

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

                this.label2.Visible =false;
                this.txtpassword.Visible = false;
                this.label3.Visible = false;
                this.txtpassword1.Visible = false;

        }

        private void txtphone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }
    }
}
